package com.zhangxu.microservice.system.web.controller.ae;

import com.zhangxu.microservice.common.context.EmployeeContextHolder;
import com.zhangxu.microservice.common.result.CommonResult;
import com.zhangxu.microservice.common.utils.ListUtils;
import com.zhangxu.microservice.system.biz.dto.AssignRoleResourceDTO;
import com.zhangxu.microservice.system.biz.dto.ResourceDTO;
import com.zhangxu.microservice.system.biz.service.RoleResourceService;
import com.zhangxu.microservice.system.web.convert.RoleResourceWebConverter;
import com.zhangxu.microservice.system.web.request.AssignRoleResourceReq;
import com.zhangxu.microservice.system.web.response.RolePermissionResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 角色资源权限管理控制器
 */
@Tag(name = "角色资源权限管理", description = "角色资源权限管理相关接口")
@RestController
@RequestMapping("/ae")
@RequiredArgsConstructor
public class RoleResourceController {

    private final RoleResourceService roleResourceService;
    private final RoleResourceWebConverter roleResourceWebConverter;

    @Operation(summary = "分配角色资源权限", description = "为角色批量分配资源权限")
    @PostMapping("role-resources/assign")
    public CommonResult<Void> assignRoleResource(@RequestBody @Validated AssignRoleResourceReq assignRoleResourceReq) {
        AssignRoleResourceDTO assignRoleResourceDTO = roleResourceWebConverter.toAssignRoleResourceDTO(assignRoleResourceReq);
        roleResourceService.assignRoleResource(assignRoleResourceDTO);
        return CommonResult.success();
    }


    @Operation(summary = "获取角色资源列表", description = "获取角色拥有的所有资源")
    @GetMapping("/roles/{roleId}/resources")
    public CommonResult<List<String>> getRoleResources(
            @Parameter(description = "角色ID", required = true, example = "1")
            @PathVariable("roleId") Long roleId) {
        List<ResourceDTO> resourceDTOList = roleResourceService.getRoleResources(roleId);
        // 这里需要使用ResourceWebConverter，暂时返回空列表
        return CommonResult.success(ListUtils.mapList(resourceDTOList, ResourceDTO::getCode));
    }

    @Operation(summary = "获取角色权限信息", description = "获取角色的完整权限信息，包括角色信息、资源列表和权限标识")
    @GetMapping("/employee/resources")
    public CommonResult<List<String>> getEmployeePermissions() {
        List<String> permissions = roleResourceService.getEmployeePermissions(EmployeeContextHolder.get().getId());
        return CommonResult.success(permissions);
    }

    @Operation(summary = "获取员工权限信息", description = "获取员工的完整权限信息")
    @GetMapping("/employees/{employeeId}/resources")
    public CommonResult<List<RolePermissionResp>> getEmployeePermissions(
            @Parameter(description = "员工ID", required = true, example = "1")
            @PathVariable("employeeId") Long employeeId) {
        List<String> permissions = roleResourceService.getEmployeePermissions(employeeId);
        // 暂时返回空列表，需要根据实际业务逻辑调整
        List<RolePermissionResp> rolePermissionRespList = Collections.emptyList();
        return CommonResult.success(rolePermissionRespList);
    }
}